import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Solution60 {
    public String getPermutation(int n, int k) {
        List<Integer> list = new ArrayList<>(n);
        for (int i = 0; i < n; i++) {
            list.add(i + 1);
        }
        for (int j = 1; j < k; j++) {
            int i = n - 1;
            while (i > 0) {
                if (list.get(i) > list.get(i - 1)) {
                    break;
                }
                i--;
            }
            for (int l = n - 1; l >= i; l--) {
                if (list.get(l) > list.get(i - 1)) {
                    Collections.swap(list, l, i - 1);
                    break;
                }
            }
            Collections.reverse(list.subList(i, n));
        }
        StringBuilder s = new StringBuilder();
        for (Integer integer : list) {
            s.append(integer);
        }
        return s.toString();
    }
}
